import {
  PagePollItemtDto,
  addPollDto,
  getPollId,
  setVoteCount,
} from '../../dto/vote/vote.dto';
import { PollService } from './poll.service';
import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
  Query,
  Req,
  Request,
  UseGuards,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiBody,
  ApiOkResponse,
  ApiOperation,
  ApiParam,
  ApiProperty,
  ApiQuery,
  ApiTags,
} from '@nestjs/swagger';
import { PaginatedResponseDto } from '../../common/class/res.class';
import { Poll } from '../../entities/poll.entity';
@ApiTags('投票管理')
@ApiBearerAuth()
@Controller('vote')
export class PollController {
  constructor(private readonly voteService: PollService) {}

  @ApiOperation({ summary: '创建投票项目' })
  @ApiBody({ type: addPollDto, description: '输入创建投票' })
  @Post('save')
  create(@Body() addPollDto, @Req() req) {
    const { userId } = req.user;
    addPollDto.userId = userId;
    // addPollDto.role = req.user.rolesIds[0];
    return this.voteService.create(addPollDto);
  }
  @ApiOperation({ summary: '投票给歌手' })
  @ApiBody({ type: setVoteCount, description: '输入歌手' })
  @Post('/setVoteCount')
  async setVote(@Body() setVoteCount, @Req() req) {
    const { userId } = req.user;
    const { singerId } = setVoteCount;
    const res = await this.voteService.vote(userId, singerId);
    console.log(res);
    return {
      code: 200,
      data: null,
      msg: res,
    };
  }

  @ApiOperation({ summary: '分页条件查询投票信息' })
  @ApiOkResponse({ type: PagePollItemtDto, description: '分页查询结果' })
  @Get('page')
  async page(
    @Query() dto: PagePollItemtDto,
    @Req() req,
  ): Promise<PaginatedResponseDto<Partial<Poll>>> {
    const { userId } = req.user;
    dto.userId = userId;
    const [records, total] = await this.voteService.page(dto);
    return {
      msg: '查询成功',
      data: {
        records,
        pageSize: dto.pageSize,
        pageNo: dto.pageNo,
        total,
      },
    };
  }
  @ApiOperation({ summary: 'id查询投票信息' })
  @ApiParam({ name: 'id', required: true, description: '投票的ID' })
  @Get('findOne/:id')
  async getFindOneId(@Param('id') id: string): Promise<any> {
    const res = await this.voteService.findOne(id);

    return res;
  }

  @ApiOperation({ summary: '查询所有投票信息' })
  @Get('findAll')
  async findAll(@Req() req): Promise<any> {
    return await this.voteService.findAll();
  }
  @ApiOperation({ summary: '投票时间趋势统计' })
  @ApiQuery({
    name: 'timeUnit',
    enum: ['day', 'week', 'month'],
    required: true,
  })
  @Get('/trend')
  async getVoteTrendByTimeUnit(
    @Query('timeUnit') timeUnit: 'day' | 'week' | 'month',
  ): Promise<any> {
    try {
      const voteTrend = await this.voteService.getVoteTrendByTimeUnit(timeUnit);
      return voteTrend;
    } catch (error) {
      // 处理错误
      return { error: 'Failed to fetch vote trend' + error };
    }
  }
  @ApiOperation({ summary: '音乐排行榜' })
  @Post('musicRanking')
  @ApiBody({ type: getPollId, description: '项目id' })
  async getMusicRanking(@Body('pollId') pollId: string): Promise<any> {
    try {
      const musicRanking = await this.voteService.getMusicRanking(pollId);
      return musicRanking;
    } catch (error) {
      // 处理错误
      return { error: 'Failed to fetch music ranking' };
    }
  }
  @ApiOperation({ summary: '歌手排行榜' })
  @ApiBody({ type: getPollId, description: '项目id' })
  @Post('singerRanking')
  async getSingerRanking(@Body('pollId') pollId: string): Promise<any> {
    try {
      const singerRanking = await this.voteService.getSingerRanking(pollId);
      return singerRanking;
    } catch (error) {
      // 处理错误
      return { error: 'Failed to fetch singer ranking' + error };
    }
  }
  @ApiOperation({ summary: '更新投票' })
  @ApiBody({ type: addPollDto, description: '输入更新投票' })
  @Post('edit')
  update(@Body() updatePollDto) {
    return this.voteService.update(updatePollDto);
  }
  @ApiOperation({ summary: '删除投票' })
  @ApiQuery({
    name: 'id',

    required: true,
  })
  @Delete('delete')
  remove(@Query('id') id: '') {
    console.log(id, 'remove(remove(');

    return this.voteService.remove(id);
  }
}
